package org.dicker.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.ReplyBiz;
import org.dicker.biz.UserBiz;
import org.dicker.biz.impl.ReplyBizImpl;
import org.dicker.biz.impl.UserBizImpl;
import org.dicker.entity.BillInfo;
import org.dicker.entity.ReplyInfo;
import org.dicker.entity.UserInfo;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestParam;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseBody;

import cn.hutool.json.JSONUtil;

@Controller("/replyapi")
public class ReplyApi {

	private ReplyBiz rBiz=new ReplyBizImpl();
	private UserBiz uBiz=new UserBizImpl();
	
	/**
	* showdoc
	* @catalog 评论模块
	* @title 用资讯id下的全部评论信息
	* @method post
	* @url http://192.168.10.6:8080/big-birds/replyapi/reply_messageapi.do
	* @param message_id 必选  int 资讯id
	* @return [{"wx_openid":"sadada","reply_id":7,"...":"..."}] 
	* @return_param array 评论列表
	* @return_param reply_id int 回复主键id
	* @return_param message_id int 资讯ID
	* @return_param user_id int 用户ID
	* @return_param content String 回复内容
	* @return_param creat_time String 回复时间
	* @return_param message_title String 标题名
	* @return_param user_name String 用户名
	* @return_param wx_openid String 微信id
	* @return_param user_img String 用户头像
	*/
	@RequestMapping(value = "/reply_messageapi.do")
	@ResponseBody
	public String reply_messageapi(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "message_id") int message_id){
		try {
			
			List<ReplyInfo> list = rBiz.selectByMessageId(message_id);
			
			
			String jsonStr = JSONUtil.toJsonStr(list);
			
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json; charset=UTF-8");
			return jsonStr;
			
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	/**
	* showdoc
	* @catalog 评论模块
	* @title 用微信id下的全部评论信息
	* @method post
	* @url http://192.168.10.6:8080/big-birds/replyapi/reply_userapi.do
	* @param wx_openid 必选  String 微信id
	* @return [{"wx_openid":"sadada","reply_id":7,"...":"..."}] 
	* @return_param array 评论列表
	* @return_param reply_id int 回复主键id
	* @return_param message_id int 资讯ID
	* @return_param user_id int 用户ID
	* @return_param content String 回复内容
	* @return_param creat_time String 回复时间
	* @return_param message_title String 标题名
	* @return_param user_name String 用户名
	* @return_param wx_openid String 微信id
	* @return_param user_img String 用户头像
	*/
	@RequestMapping(value = "/reply_userapi.do")
	@ResponseBody
	public String reply_userapi(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "wx_openid") String wx_openid){
		try {
			
			List<ReplyInfo> list = rBiz.selectByUser(wx_openid);
			
			
			String jsonStr = JSONUtil.toJsonStr(list);
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json; charset=UTF-8");
			
			return jsonStr;
			
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	
	
	/**
	* showdoc
	* @catalog 评论模块
	* @title 添加评论
	* @method post
	* @url http://192.168.10.6:8080/big-birds/replyapi/reply_addapi.do
	* @param wx_openid 必选  String 微信id
	* @param message_id 必选  String 资讯id
	* @param content 必选  String 回复内容
	* @return 1/0 1是添加成功/0是添加失败 
	*/
	@RequestMapping(value = "/reply_addapi.do")
	@ResponseBody
	public String reply_addapi(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "wx_openid") String wx_openid,
			@RequestParam(name = "message_id") String message_id,
			@RequestParam(name = "content") String content){
		
		String str="0";
		try {
			
			if(content!=null){
				UserInfo uInfo = uBiz.selectUserid(wx_openid);
				
				ReplyInfo rInfo = new ReplyInfo();
				rInfo.setContent(content);
				rInfo.setMessage_id(Integer.parseInt(message_id));
				rInfo.setUser_id(uInfo.getUser_id());
				if(rBiz.addReply(rInfo)){
					str="1";
				}else{
					str="0";
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	* showdoc
	* @catalog 评论模块
	* @title 查询资讯id下有多少条评论
	* @method post
	* @url http://192.168.10.6:8080/big-birds/replyapi/reply_countapi.do
	* @param message_id 必选  String 资讯id
	* @return 返回一个数
	*/
	@RequestMapping(value = "/reply_countapi.do")
	@ResponseBody
	public String reply_countapi(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam(name = "message_id") String message_id){
		
		String str="0";
		try {
			
			if(message_id!=null){
				
				int num=rBiz.selectCount(Integer.parseInt(message_id));
				str = num +"";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	
}
